﻿//-----------------------------------------------------------------------
// <copyright file="Backup.cs" company="CKS.Office365">
// CKS Office365 CodePlex
// </copyright>
//-----------------------------------------------------------------------

namespace CKS.Office365.Prerequisite
{
    using System.Linq;
    using System.Security.Permissions;
    using CKS.Office365.Diagnostics;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Security;

    /// <summary>
    /// A helper class for backing up lists and folders
    /// </summary>
    internal static class Backup
    {
        /// <summary>
        /// Backups the folders.
        /// </summary>
        /// <param name="webContext">The web context.</param>
        /// <param name="instanceId">The instance id.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        internal static void Folders(SPWeb webContext, string instanceId)
        {
            RenameFolder(webContext, "Documents", "Documents_" + instanceId);
            RenameFolder(webContext, "Images", "Images_" + instanceId);
            RenameFolder(webContext, "Pages", "Pages_" + instanceId);
            RenameFolder(webContext, "SiteImages", "SiteImages_" + instanceId);
            RenameFolder(webContext, "TemplateGallery", "TemplateGallary_" + instanceId);
        }

        /// <summary>
        /// Backups the lists.
        /// </summary>
        /// <param name="webContext">The web context.</param>
        /// <param name="instanceId">The instance id.</param>
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        internal static void Lists(SPWeb webContext, string instanceId)
        {
            RenameList(webContext, "Documents", "Documents_" + instanceId);
            RenameList(webContext, "Web Pages", "Web Pages_" + instanceId);
            RenameList(webContext, "Templates", "Templates_" + instanceId);
            RenameList(webContext, "Images", "Images_" + instanceId);
        }

        /// <summary>
        /// Renames the list.
        /// </summary>
        /// <param name="webContext">The web context.</param>
        /// <param name="listTitle">The list title.</param>
        /// <param name="newTitle">The new title.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "CKS.Office365.Diagnostics.Log.QuickLogInformational(Microsoft.SharePoint.SPWeb,System.String,System.Object[])", Justification = "Reviewed, only used for debugging.")]
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        private static void RenameList(SPWeb webContext, string listTitle, string newTitle)
        {
            SPList list = webContext.Lists.TryGetList(listTitle);

            if (list != null)
            {
                list.Title = newTitle;
                list.Update();
                Log.QuickLogInformational(webContext, "Changed the name of list {0} to {1}", listTitle, newTitle);
            }
        }

        /// <summary>
        /// Renames the folder.
        /// </summary>
        /// <param name="webContext">The web context.</param>
        /// <param name="folderName">Name of the folder.</param>
        /// <param name="newFolderName">New name of the folder.</param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1303:Do not pass literals as localized parameters", MessageId = "CKS.Office365.Diagnostics.Log.QuickLogInformational(Microsoft.SharePoint.SPWeb,System.String,System.Object[])", Justification = "Reviewed, only used for debugging.")]
        [SharePointPermission(SecurityAction.LinkDemand, ObjectModel = true)]
        private static void RenameFolder(SPWeb webContext, string folderName, string newFolderName)
        {
            // TODO: change this to check correctly.
            SPFolder folder = webContext.Folders.Cast<SPFolder>().FirstOrDefault(p => p.Name == folderName);
            if (folder != null)
            {
                folder.MoveTo(newFolderName);
                Log.QuickLogInformational(webContext, "Moved folder {0} to {1}", folder, newFolderName);
            }
        }
    }
}
